package com.tushu;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.tushu.dao.AdminDao;
import com.tushu.dao.StudentDao;
import com.tushu.model.Admin;
import com.tushu.model.Student;
import com.tushu.view.AdminWin;
import com.tushu.view.StuWin;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField account;
	private JPasswordField password;
	
	StudentDao studentDao=new StudentDao();
	AdminDao adminDao=new AdminDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		try {
			UIManager.setLookAndFeel(   UIManager.getSystemLookAndFeelClassName()); 
		} catch (Exception e) {
			// TODO: handle exception
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 415, 299);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u501F\u9605\u7CFB\u7EDF");
		lblNewLabel.setFont(new Font("????", Font.PLAIN, 18));
		lblNewLabel.setBounds(138, 20, 147, 34);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u8D26\u53F7\uFF1A");
		lblNewLabel_1.setFont(new Font("????", Font.PLAIN, 13));
		lblNewLabel_1.setBounds(91, 76, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel_2.setFont(new Font("????", Font.PLAIN, 13));
		lblNewLabel_2.setBounds(91, 124, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		account = new JTextField();
		account.setBounds(138, 73, 133, 21);
		contentPane.add(account);
		account.setColumns(10);
		
		password = new JPasswordField();
		password.setBounds(138, 121, 130, 21);
		contentPane.add(password);
		
		JLabel lblNewLabel_3 = new JLabel("\u8EAB\u4EFD\uFF1A");
		lblNewLabel_3.setFont(new Font("????", Font.PLAIN, 13));
		lblNewLabel_3.setBounds(91, 162, 58, 15);
		contentPane.add(lblNewLabel_3);
		
		JRadioButton rb1 = new JRadioButton("\u8BFB\u8005",true);
		rb1.setFont(new Font("????", Font.PLAIN, 13));
		rb1.setBounds(141, 158, 66, 23);
		contentPane.add(rb1);
		
		JRadioButton rb2 = new JRadioButton("\u7BA1\u7406\u5458",false);
		rb2.setFont(new Font("????", Font.PLAIN, 13));
		rb2.setBounds(220, 158, 93, 23);
		contentPane.add(rb2);
		
		JButton btnNewButton = new JButton("\u91CD\u7F6E");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				????
				account.setText("");
				password.setText("");
				rb1.setSelected(true);
				rb2.setSelected(false);
			}
		});
		btnNewButton.setBounds(69, 202, 97, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u767B\u5F55");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String accountString=account.getText();
				String pswString=String.valueOf(password.getPassword());
//				??????
				if(rb1.isSelected()) {
//					????
					Student student=studentDao.getStudentByAccount(accountString);
					if (student==null) {
						JOptionPane.showMessageDialog(null,"error");
					}
					else if (student.getPassword().equals(pswString)) {
						try {
							new StuWin(student).setVisible(true);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						setVisible(false);
					}else {
						JOptionPane.showMessageDialog(null,"error");
					}
				}
				if(rb2.isSelected()) {
//					?????
					Admin admin=adminDao.selectAdmin(accountString);
					if (admin==null) {
						JOptionPane.showMessageDialog(null,"error");
					}
					else if (admin.getPassword().equals(pswString)) {
						new AdminWin().setVisible(true);
						setVisible(false);
					}else {
						JOptionPane.showMessageDialog(null,"error");
					}
				}
			}
		});
		btnNewButton_1.setBounds(216, 202, 97, 23);
		contentPane.add(btnNewButton_1);
		
		ButtonGroup group=new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
	}
}
